<?php

	if(isset($_REQUEST["key"]))
	{
		$key=$_REQUEST["key"];
		$type=$_REQUEST["type"];
		
		$sql="Select distinct * From gian_hang where gh_ten like '%$key%' or gh_ngay_tao='$key' or gh_header like '%$key%' or gh_dia_chi like '%$key%'";
		$sql1="Select distinct count(*) as soluong From gian_hang where gh_ten like '%$key%' or gh_ngay_tao='$key' or gh_header like '%$key%' or gh_dia_chi like '%$key%'";
	
	
		$ngay="";
		$tengh="";
		
		switch($type)
		{
			case 0:
				$sql=$sql." order by gh_ngay_tao DESC,gh_ten ASC";
				$ngay="selected='selected'";
				$tengh="";
				break;
			case 1:
				$sql=$sql." order by gh_ten ASC";
				$ngay="";
				$tengh="selected='selected'";
				break;
			
	
		}
		//echo($sql);
		require("DataHelper.php");
		$maxPage=0;
		$rowPerPage=10;
		$curPage=1;
		$maxItems=0;
		//echo($sql1);
		$result=QuerySQL($sql1);
		//echo($result);
		if($row=mysql_fetch_array($result))
		{

			$maxItems=$row["soluong"];

			$maxPage=ceil($maxItems/$rowPerPage);
				
		}
				
		
		if(isset($_REQUEST["page"]))
		{
				$curPage=$_REQUEST["page"];
		}
		$start=($curPage-1)*$rowPerPage;
		$end=($curPage-1)*$rowPerPage+$rowPerPage;
		$i=$start;
		$str="";
		if($maxItems==0)
		{
			$str="<hr/><h3>Không tìm thấy gian hàng nào phù hợp</h3><br/>";
		}
		else
		{
			$str="<hr/><h3>Tìm thấy $maxItems gian hàng phù hợp</h3><br/>";
			$str=$str."<label>Sắp xếp theo</label>
			<select  onchange='cbChangegh();' id='slid'>
                	<option value='0' $ngay>Ngày tạo</option>
                    <option value='1' $tengh>Tên gian hàng</option>
                </select><br/><br/>";
		}
		$str=$str."<table>
				<tbody>";
		$sql=$sql." Limit $start,$rowPerPage";
		//echo($sql);		
		$result1=QuerySQL($sql);
		while($row=mysql_fetch_array($result1))
		{
		
			$ma=$row["gh_ma"];
			$ten=$row["gh_ten"];
			$ngaytao=$row["gh_ngay_tao"];
			$header=$row["gh_header"];
			$banner="images/".$row["gh_banner"];
			$footer=$row["gh_footer"];
			$diachi=$row["gh_dia_chi"];
			$username=$row["gh_username"];
			$sql2="Select distinct count(*) as soluong From thich_gian_hang where tgh_ma_gian_hang=$ma Group by tgh_ma_gian_hang";
			$result2=QuerySQL($sql2);
			if($sl=mysql_fetch_array($result2))
				$soluong=$sl["soluong"];
			else
				$soluong=0;
			
			
			$str=$str."<tr>
							<td style='align='center'>
								<img src='$banner' with='200px' height='80px' alt='banner gian hàng'/>
							</td>
						</tr>
						<tr>
							<td>
								<label class='stitle'>Tên gian hàng :</label>
								<label class='sproduct'><a href='#'>$ten</a></label>
							</td>
						</tr>
						<tr>
							<td>
								<label class='stitle'>Chủ gian hàng :</label>
								<label class='sproduct'><a href='#'>$username</a></label>
							</td>
						</tr>
						<tr>
							<td>
								<label class='stitle'>Ngày tạo :</label>
								<label class='sproduct'>$ngaytao</label>
							</td>
						</tr>
						<tr>
							<td>
								<label class='stitle'>Số lượng người thích:</label>
								<label class='sproduct'>$soluong người</label>
								<br/><br/><br/>
							</td>
						</tr>
							
					";
				
		}
		$str=$str."</tbody>
			</table><br/>";
		if($maxItems>0)
		{
			$str=$str."<table><tr><td>Trang :";
			for($i=1;$i<=$maxPage;$i++)
			{
				if($i==$curPage)
				{
					$str=$str."<b id='currentPage' title='$i' style='color:red'>$i&nbsp;&nbsp;</b>";
				}
				else
				{
					$str=$str."<label onclick='indexClickgh($i);' style='text-decoration:none;cursor:pointer;'>$i&nbsp;&nbsp;</label>";
				}
			}
		}
		$str=$str."</table><input type='hidden' value='$key' id='keyhd' />";
		$str=$str."<input type='hidden' value='$type' id='typehd' />";
		echo($str);
	}
?>